package com.github.prontera.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.RequestLogging;
import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.domain.ClientFollow;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.service.ClientFollowService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * 关注
 */
@RestController
@Api(value = "关注/粉丝")
@RequestMapping(value = ApiVersion.API_V1_PREFIX+"/follow")
public class ClientFollowController{

	@Autowired
	private ClientFollowService clientFollowService;
	
	/**
	 * 我的关注
	 */
	@RequestMapping(value = "clientFollow", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "我的关注" , notes = "我的关注")
	public ObjectDataResponse<List<ClientFollow>> clientFollow(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody ClientFollow clientFollow){
		clientFollow.setClientId(clientId);
		clientFollow.setFollowId(clientId);
		List<ClientFollow> datas=clientFollowService.findClientFollow(clientFollow);
		return new ObjectDataResponse<List<ClientFollow>>(datas);
	}
	
	/**
	 *获取当前用户得关注关系
	 */
	
	@RequestMapping(value = "getFollow", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "获取当前用户得关注关系" , notes = "获取余当前用户得关注关系")
	public ObjectDataResponse<ClientFollow> getFollow(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody ClientFollow clientFollow){
		clientFollow.setFollowId(clientId);
		ClientFollow findFollow=clientFollowService.getFollow(clientFollow);
		if(findFollow==null){
			return new ObjectDataResponse<ClientFollow>(new ClientFollow());
		}
		return new ObjectDataResponse<ClientFollow>(findFollow);
	}
	
	/**
	 * 我的粉丝
	 */
	
	@RequestMapping(value = "clientFans", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "我的粉丝" , notes = "我的粉丝")
	public ObjectDataResponse<List<ClientFollow>> clientFans(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody ClientFollow clientFollow){
		clientFollow.setClientId(clientId);
		clientFollow.setFollowId(clientId);
		List<ClientFollow> datas=clientFollowService.findClientFans(clientFollow);
		return new ObjectDataResponse<List<ClientFollow>>(datas);
	}
	
	/**
	 * 添加关注
	 */
	@RequestMapping(value = "addFollow", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加关注" , notes = "添加关注")
	public ObjectDataResponse<Boolean> add(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody @ApiParam(hidden=true,name="对方账户ID") Long followId){
		ClientFollow clientFollow=new ClientFollow();
		clientFollow.setFollowId(followId);
		clientFollow.setClientId(clientId);
		if(clientFollow.getFollowId()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "followId必传!");
		}
		boolean success=clientFollowService.insertFollow(clientFollow);
		return new ObjectDataResponse<Boolean>(success);
	}

	/**
	 * 取消关注
	 */
	@RequestMapping(value = "deleteFollow", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "取消关注" , notes = "取消关注")
	public ObjectDataResponse<Boolean> delete(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody  @ApiParam(hidden=true,name="对方账户ID") Long followId){
		ClientFollow clientFollow=new ClientFollow();
		clientFollow.setFollowId(followId);
		clientFollow.setClientId(clientId);
		if(clientFollow.getFollowId()==null) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "followId必传!");
		}
		boolean success=clientFollowService.removeFollow(clientFollow);
		return new ObjectDataResponse<Boolean>(success);
	}
}
